package com.ybkj.o2o.mc.service.impl;


import com.ybkj.o2o.mc.common.query.PassengerQuery;
import com.ybkj.o2o.mc.common.query.PassengerQuery2;
import com.ybkj.o2o.mc.entity.Passenger;
import com.ybkj.o2o.mc.mapper.PassengerAddressMapper;
import com.ybkj.o2o.mc.mapper.PassengerConcatsMapper;
import com.ybkj.o2o.mc.mapper.PassengerMapper;
import com.ybkj.o2o.mc.service.PassengerService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by shine on 15/8/19.
 */
@Service
public class DefaultPassengerService implements PassengerService {

    @Autowired
    private PassengerMapper passengerMapper;

    @Autowired
    private PassengerConcatsMapper passengerConcatsMapper;

    @Autowired
    private PassengerAddressMapper passengerAddressMapper;

    @Override
    public boolean existsByPhone(String phone, String appKey) {

        Passenger passenger = passengerMapper.selectByPhoneAndAppKey(phone, appKey);

        return null != passenger;
    }

    @Override
    public boolean existsByIdcard(String idcard, String appKey) {

        Passenger passenger = passengerMapper.selectByIdcardAndAppKey(idcard, appKey);

        return null != passenger;
    }

    @Override
    public boolean existsByEmail(String email, String appKey) {

        Passenger passenger = passengerMapper.selectByEmailAndAppKey(email, appKey);

        return null != passenger;
    }

    @Override
    public Passenger findByIdAndAppKey(Long passengerId, String appKey) {

        return passengerMapper.selectByIdAndAppkey(passengerId, appKey);
    }

    @Override
    public Passenger findByPhoneAndAppKey(String phone, String appKey) {

        return passengerMapper.selectByPhoneAndAppKey(phone, appKey);
    }

    @Override
    public Passenger findByJobNumberAndAppKey(String jobNumber, String appKey) {
        return passengerMapper.selectByJobNumberAndAppKey(jobNumber, appKey);
    }

    @Override
    public Passenger findByNameAndAppKey(String name, String appKey) {
        return passengerMapper.selectByJobNumberAndAppKey(name, appKey);
    }

    @Override
    public Passenger findByEmailAndAppKey(String email, String appKey) {

        return passengerMapper.selectByEmailAndAppKey(email, appKey);
    }

    @Override
    public Passenger findByIdcardAndAppKey(String idcard, String appKey) {

        return passengerMapper.selectByIdcardAndAppKey(idcard, appKey);
    }

    @Override
    public int insert(Passenger passenger) {
        if (null == passenger.getCreated()) {
            passenger.setCreated(new Date());
        }
        if (null == passenger.getUpdated()) {
            passenger.setUpdated(new Date());
        }
        passenger.setVersion(1);
        return passengerMapper.insert(passenger);
    }

    @Override
    public int update(Passenger passenger) {

        passenger.setUpdated(new Date());
        return passengerMapper.update(passenger);
    }

    @Override
    public int updateByDate(Passenger passenger) {

        passenger.setUpdated(new Date());
        return passengerMapper.updateByDate(passenger);
    }

    @Override
    public int delete(Long passengerId, String appKey) {

        return passengerMapper.delete(passengerId, appKey);
    }


    @Override
    public int updateLocation(Passenger passenger) {

        return passengerMapper.updateLocation(passenger);
    }

    @Override
    public int updateBlackList(Passenger passenger) {

        return passengerMapper.updateBlackList(passenger);
    }

    @Override
    public int updateDeviceType(Passenger passenger) {

        return passengerMapper.updateDeviceType(passenger);
    }

    @Override
    public int updateBalance(Passenger passenger) {

        Assert.notNull(passenger.getAppKey());
        Assert.notNull(passenger.getId());
        if (null == passenger.getVersion()) {
            passenger.setVersion(1);
        }

        return passengerMapper.updateBalance(passenger);
    }

    @Override
    public List<Passenger> findByPassengerQuery(PassengerQuery passengerQuery) {

        return passengerMapper.selectByPassengerQuery(passengerQuery);
    }

    @Override
    public long countByPassengerQuery(PassengerQuery passengerQuery) {

        return passengerMapper.countByPassengerQuery(passengerQuery);
    }

    @Override
    public List<String> findChannels(String appKey) {

        return passengerMapper.findChannels(appKey);
    }

    @Override
    public double sumDrawMoney(Long companyId, String passengerName, String appKey) {

        return passengerMapper.sumDrawMoney(companyId, passengerName, appKey);
    }

    @Override
    public Passenger fetch(Long id) {

        return passengerMapper.selectByPrimarykey(id);
    }

    @Override
    public List<Passenger> findByPassengerQuery2(PassengerQuery2 passengerQuery) {
        return passengerMapper.selectByPassengerQuery2(passengerQuery);
    }

    @Override
    public long countByPassengerQuery2(PassengerQuery2 passengerQuery) {

        return passengerMapper.countByPassengerQuery2(passengerQuery);
    }

    @Override
    public List<Passenger> findByPassengerQuery3(PassengerQuery2 passengerQuery) {
        return passengerMapper.selectByPassengerQuery3(passengerQuery);
    }

  @Override
  public long countByPassengerQuery3(PassengerQuery2 passengerQuery) {

    return passengerMapper.countByPassengerQuery3(passengerQuery);
  } 

  @Override
  public List<Passenger> findByTypeOrCompanyId(Integer type, Long companyId, String appKey) {

    Assert.notNull(appKey);

    return passengerMapper.selectByTypeAndCompanyId(type, companyId, appKey);
  }

  @Override
  public List<Passenger> findByEnterpriseAndAppkey(Long enterpriseId, String appKey) {

    return passengerMapper.selectByEnterpriseAndAppkey(enterpriseId, appKey);
  }

  @Override
  public int deleteByEnterpriseAndAppkey(Long enterpriseId, String appKey) {

    return passengerMapper.deleteByEnterpriseAndAppkey(enterpriseId, appKey);
  }

  @Override
  public Long countByEnterprise(Long enterpriseId) {

    return passengerMapper.countByEnterprise(enterpriseId);
  }

  @Override
  public List<Passenger> findByEnterpriseForPage(Long enterpriseId, int offset, int rows) {

    return passengerMapper.selectByEnterpriseForPage(enterpriseId, offset, rows);

  }

  @Override
  public List<Passenger> findByPhones(List<String> phones, String appKey) {

    return passengerMapper.selectByPhones(phones, appKey);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public int batchInsert(List<Passenger> passengers) {

    return passengerMapper.batchInsert(passengers);
  }

  @Override
  public int batchDelete(List<Long> deleteList) {

    //删除常用联系人
    passengerConcatsMapper.deleteByPassengers(deleteList);
    //删除常用地址
    passengerAddressMapper.deleteByPassengers(deleteList);

    //TODO 删除财务记录

    return passengerMapper.batchDelete(deleteList);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public int batchUpdate(List<Passenger> passengers) {
    if (CollectionUtils.isEmpty(passengers)) {
      return 0;
    }
    return passengerMapper.batchUpdate(passengers);
  }

  @Override
  public int updateCompanyName(Long companyId, String abbreviation, String appKey) {

    return passengerMapper.updateCompanyName(companyId, abbreviation, appKey);
  }

  @Override
  public List<Passenger> findByGradeId(Long gradeId,String appKey){

    return passengerMapper.findByGradeId(gradeId,appKey);
  }

  @Override
  public int batchUpdateGrade(Long gradeId, String appKey) {

    return passengerMapper.batchUpdateGrade(gradeId,appKey);
  }

  @Override
  public int disposeGrade(Long gradeId, String gradeName, Long companyId, String appKey) {

        return passengerMapper.disposeGrade(gradeId, gradeName, companyId, appKey);
    }

    @Override
    public List<Passenger> findByIds(PassengerQuery passengerQuery) {

        return passengerMapper.findByIds(passengerQuery);
    }

    @Override
    public Long countByIds(PassengerQuery passengerQuery) {

        return passengerMapper.countByIds(passengerQuery);
    }

    @Override
    public int updateByDelDepartment(Long id, String appKey) {
        // TODO Auto-generated method stub
        return passengerMapper.updateByDelDepartment(id, appKey);
    }

    @Override
    public int batchUpdateByIds(List<Long> passengerIds, String appKey) {
        return passengerMapper.batchUpdateByIds(passengerIds, appKey);
    }

    @Override
    public int updateEnterpriseName(Long enterpriseId, String enterpriseName,
                                    String appKey) {
        return passengerMapper.updateEnterpriseName(enterpriseId, enterpriseName, appKey);
    }

    @Override
    public int updateDepartmentName(Long departmentId, String departmentName,
                                    String appKey) {
        return passengerMapper.updateDepartmentName(departmentId, departmentName, appKey);
    }

    @Override
    public int updateGradeByEnterpriseId(Long enterpriseId, Long gradeId,
                                         String gradeName, String appKey) {
        return passengerMapper.updateGradeByEnterpriseId(enterpriseId, gradeId, gradeName, appKey);
    }

    @Override
    public int updateAllowOverdraw(Long enterpriseId, Boolean allowOverdraw,
                                   String appKey) {
        return passengerMapper.updateAllowOverdraw(enterpriseId, allowOverdraw, appKey);
    }

    @Override
    public Long findByIntroduceType(Integer introduceType,
                                            String introducer, Integer companyId, String appKey) {
        return passengerMapper.findByIntroduceType(introduceType, introducer, companyId, appKey);
    }

    @Override
    public int updateChannel(String channelName, Long id, String appKey) {
        return passengerMapper.updateChannel(id, channelName, appKey);
    }

    @Override
    public List<Map<String, Object>> findWeixinInfoByIds(List<Long> ids){
        return passengerMapper.findWeixinInfoByIds(ids);
    }

    @Override
    public List<Passenger> findPassengersWithIntroducerBeforeDate(PassengerQuery passengerQuery){
        return passengerMapper.findPassengersWithIntroducerBeforeDate(passengerQuery);
    }

    @Override
    public int updateWxInfoByPhone(String wxHeadImgUrl, String wxNickName, String phone) {
        return passengerMapper.updateWxInfoByPhone(wxHeadImgUrl, wxNickName, phone);
    }
}
